package com.ruoyi.ca.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.ruoyi.ca.util.SnowflakeUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ca.mapper.CaJylshMapper;
import com.ruoyi.ca.domain.CaJylsh;
import com.ruoyi.ca.service.ICaJylshService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 交易流水号信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-16
 */
@Service
public class CaJylshServiceImpl implements ICaJylshService {
    @Autowired
    private CaJylshMapper caJylshMapper;

    /**
     * 查询交易流水号信息
     *
     * @param id 交易流水号信息主键
     * @return 交易流水号信息
     */
    @Override
    public CaJylsh selectCaJylshById(Long id) {
        return caJylshMapper.selectCaJylshById(id);
    }

    /**
     * 查询交易流水号信息列表
     *
     * @param caJylsh 交易流水号信息
     * @return 交易流水号信息
     */
    @Override
    public List<CaJylsh> selectCaJylshList(CaJylsh caJylsh) {
        return caJylshMapper.selectCaJylshList(caJylsh);
    }

    /**
     * 新增交易流水号信息
     *
     * @param caJylsh 交易流水号信息
     * @return 结果
     */
    @Override
    public int insertCaJylsh(CaJylsh caJylsh) {
        caJylsh.setCreateTime(DateUtils.getNowDate());
        return caJylshMapper.insertCaJylsh(caJylsh);
    }

    /**
     * 修改交易流水号信息
     *
     * @param caJylsh 交易流水号信息
     * @return 结果
     */
    @Override
    public int updateCaJylsh(CaJylsh caJylsh) {
        caJylsh.setUpdateTime(DateUtils.getNowDate());
        return caJylshMapper.updateCaJylsh(caJylsh);
    }

    /**
     * 批量删除交易流水号信息
     *
     * @param ids 需要删除的交易流水号信息主键
     * @return 结果
     */
    @Override
    public int deleteCaJylshByIds(Long[] ids) {
        return caJylshMapper.deleteCaJylshByIds(ids);
    }

    /**
     * 删除交易流水号信息信息
     *
     * @param id 交易流水号信息主键
     * @return 结果
     */
    @Override
    public int deleteCaJylshById(Long id) {
        return caJylshMapper.deleteCaJylshById(id);
    }

    /**
     * 交易流水号信息导入
     *
     * @param file
     * @param operName
     * @return
     */
    @Override
    @Transactional
    public String importCaJylsh(MultipartFile file, String operName) {

        try {
            List<CaJylsh> list = EasyExcel.read(file.getInputStream()).head(CaJylsh.class).sheet().doReadSync();
            List<List<CaJylsh>> partition = Lists.partition(list, 500);
            Snowflake snowflake = IdUtil.createSnowflake(1, 1);
            for (List<CaJylsh> caJylshsList : partition){

                // 查询已经存在的数据
                List<String> result = caJylshMapper.queryByJylsh(caJylshsList);

                List<CaJylsh> updateList = caJylshsList.stream().filter(caJylsh -> result.contains(caJylsh.getJylsh())).collect(Collectors.toList());
                List<CaJylsh> insertList = caJylshsList.stream().filter(caJylsh -> !result.contains(caJylsh.getJylsh())).collect(Collectors.toList());

                // 批量保存数据
                if (CollectionUtil.isNotEmpty(insertList)){
                    insertList.forEach(caJylsh -> {
                        caJylsh.setId(snowflake.nextId());
                        caJylsh.setCreateBy(operName);
                        caJylsh.setCreateTime(DateUtils.getNowDate());
                    });
                    caJylshMapper.batchInsertCaJylsh(insertList);
                }

                // 批量更新数据
                if (CollectionUtil.isNotEmpty(updateList)){
                    updateList.forEach(caJylsh -> {
                        caJylsh.setId(snowflake.nextId());
                        caJylsh.setUpdateBy(operName);
                        caJylsh.setUpdateTime(DateUtils.getNowDate());
                    });
                    caJylshMapper.batchUpdateCaJylsh(updateList);
                }
            }


        } catch (IOException e) {
            throw new ServiceException("交易流水号信息导入失败，失败原因：" + e.getMessage());
        }

        return "交易流水号信息导入成功！";
    }

    /**
     * 根据流水号信息查询
     * @param jylsh
     * @return
     */
    @Override
    public CaJylsh selectByJylsh(String jylsh) {
        return caJylshMapper.selectByJylsh(jylsh);
    }
}